package com.ctsi.ssdc.web;

import java.net.URI;
import java.net.URISyntaxException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Optional;
import java.lang.Integer;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.data.domain.Pageable;
import org.springframework.http.ResponseEntity;
import org.springframework.util.Assert;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import com.ctsi.ssdc.service.LitemallCollectService;
import com.ctsi.ssdc.domain.LitemallCollect;

import com.ctsi.ssdc.exception.BadRequestAlertException;
import com.ctsi.ssdc.model.PageResult;
import com.ctsi.ssdc.util.HeaderUtil;
import com.ctsi.ssdc.util.ResponseUtil;


/**
 * REST controller for managing LitemallCollect.
 *
 * @author ctsi-biyi-generator
 *
 */
@Api(value = "/api",tags = {"litemall-collect-controller"})
@RestController
@RequestMapping("/api")
public class LitemallCollectController {

    private final Logger log = LoggerFactory.getLogger(LitemallCollectController.class);

    private static final String ENTITY_NAME = "litemallCollect";

    private final LitemallCollectService litemallCollectService;

    public LitemallCollectController(LitemallCollectService litemallCollectService) {
        this.litemallCollectService = litemallCollectService;
    }

    @InitBinder   
    public void initBinder(WebDataBinder binder) {   
        DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");   
        dateFormat.setLenient(true);   
        binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, true));   
    } 
   
    /**
     * POST  /litemallCollects : Create a new litemallCollect.
     *
     * @param litemallCollect the litemallCollect to create
     * @return the ResponseEntity with status 201 (Created) and with body the new litemallCollect,
        or with status 400 (Bad Request) if the litemallCollect has already an id
     * @throws URISyntaxException if the Location URI syntax is incorrect
     */
    @ApiImplicitParams({
        @ApiImplicitParam(paramType = "body",dataType = "LitemallCollect",name = "litemallCollect",value = "the litemallCollect to create")
    })
    @ApiOperation(value = "POST  /litemallCollects : create a new litemallCollect.",
        notes = "POST  /litemallCollects : create a new litemallCollect.",httpMethod = "POST")
    @PostMapping("/litemallCollects")
    public ResponseEntity<LitemallCollect> createLitemallCollect(@RequestBody LitemallCollect litemallCollect) throws URISyntaxException {
        log.debug("REST request to save LitemallCollect : {}", litemallCollect);
        if (litemallCollect.getId() != null) {
            throw new BadRequestAlertException("A new litemallCollect cannot already have an id", ENTITY_NAME, "idexists");
        }

        boolean save =  litemallCollectService.save(litemallCollect);
        Assert.isTrue(save,"Insert failed");
        LitemallCollect result=litemallCollect;

        return ResponseEntity.created(new URI("/api/litemallCollects" + "/" +result.getId() ))
            .headers(HeaderUtil.createEntityCreationAlert(ENTITY_NAME, result.getId().toString()))
            .body(result);
    }
	
    /**
     * PUT  /litemallCollects : Updates an existing litemallCollect.
     *
     * @param litemallCollect the litemallCollect to update
     * @return the ResponseEntity with status 200 (OK) and with body the updated litemallCollect,
     * or with status 400 (Bad Request) if the litemallCollect is not valid,
     * or with status 500 (Internal Server Error) if the litemallCollect couldn't be updated
     * @throws URISyntaxException if the Location URI syntax is incorrect
     */
    @ApiImplicitParams({
        @ApiImplicitParam(paramType = "body",dataType = "LitemallCollect",name = "litemallCollect",value = "the litemallCollect to update")
    })
    @ApiOperation(value = "PUT  /litemallCollects : updates an existing litemallCollect.",
        notes = "PUT  /litemallCollects : updates an existing litemallCollect.",httpMethod = "PUT")
    @PutMapping("/litemallCollects")
    public ResponseEntity<LitemallCollect> updateLitemallCollect(@RequestBody LitemallCollect litemallCollect) throws URISyntaxException {
        log.debug("REST request to update LitemallCollect : {}", litemallCollect);
	    if (litemallCollect.getId() == null) {
	    	return createLitemallCollect(litemallCollect);
	    }

        boolean update = litemallCollectService.updateById(litemallCollect);
        Assert.isTrue(update,"Update failed");
        LitemallCollect result=litemallCollect;

        return ResponseEntity.ok()
            .headers(HeaderUtil.createEntityUpdateAlert(ENTITY_NAME, result.getId().toString()))
            .body(result);
    }

    /**
     * GET  /litemallCollects/:id : get the "id" litemallCollect.
     *
     * @param id the id of the litemallCollect to retrieve
     * @return the ResponseEntity with status 200 (OK) and with body the litemallCollect, or with status 404 (Not Found)
     */
    @ApiImplicitParams({
        @ApiImplicitParam(paramType = "path",dataType = "INTEGER",
        name = "id",value = "the id of the litemallCollect to retrieve")
    })
    @ApiOperation(value = "GET  /litemallCollects/id : get the id litemallCollect.",
        notes = "GET  /litemallCollects/id : get the id litemallCollect.",httpMethod = "GET")
    @GetMapping("/litemallCollects/{id}")
    public ResponseEntity<LitemallCollect> getLitemallCollect(@PathVariable Integer id) {
        log.debug("REST request to get LitemallCollect : {}", id);
        LitemallCollect litemallCollect = litemallCollectService.getById(id);
        return ResponseUtil.wrapOrNotFound(Optional.ofNullable(litemallCollect));
    }

    /**
    * GET  /litemallCollects : get the litemallCollects firstStringBaseColumn.
    *
    * @return the ResponseEntity with status 200 (OK) and the list of litemallCollects in body
    */
    @ApiOperation(value = "GET  /litemallCollects/id : get the id litemallCollect.",
        notes = "GET  /litemallCollects/id : get the id litemallCollect.",httpMethod = "GET")
    @GetMapping("/litemallCollects")
    public PageResult<LitemallCollect> getLitemallCollectsList(String str,Pageable pageable) {
        log.debug("REST request to get LitemallCollectsList");
        return litemallCollectService.findFirstStringColumn(str,pageable);
    }
	
    /**
     * DELETE  /litemallCollects/:id : delete the "id" litemallCollect.
     *
     * @param id the id of the litemallCollect to delete
     * @return the ResponseEntity with status 200 (OK)
     */
    @ApiImplicitParams({
        @ApiImplicitParam(paramType = "path",dataType = "INTEGER",name = "id",
        value = "the id of the litemallCollect to delete")
    })
    @ApiOperation(value = "DELETE  /litemallCollects/id : delete the id litemallCollect.",
        notes = "DELETE  /litemallCollects/id : delete the id litemallCollect.",httpMethod = "DELETE")
    @DeleteMapping("/litemallCollects/{id}")
    public ResponseEntity<Void> deleteLitemallCollect(@PathVariable Integer id) {
        log.debug("REST request to delete LitemallCollect : {}", id);
        litemallCollectService.removeById(id);
        return ResponseEntity.ok().headers(HeaderUtil.createEntityDeletionAlert(ENTITY_NAME, id.toString())).build();
    }
    
}
